package com.ruima.erp.framework.db.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.sql.rowset.CachedRowSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ruima.erp.framework.dao.DaoUtil;
import com.ruima.erp.framework.dao.IRmSysUser;
import com.ruima.erp.framework.entity.RmSysUser;

public class RmSysUserImpl implements IRmSysUser {

	
	public List<RmSysUser> getRmSysUsers() throws SQLException{
		List<RmSysUser> list = new ArrayList<RmSysUser>();
		
		String execSql = "select * from rm_sys_user";
		
		CachedRowSet crs = DaoUtil.SimpleQuery(execSql); 
		
		while(crs.next()){
			RmSysUser rmSysUser  = new RmSysUser();
			rmSysUser.setObjid(crs.getString("objid"));
			rmSysUser.setDisindex(crs.getString("disindex"));
			rmSysUser.setName(crs.getString("name"));
			rmSysUser.setCode(crs.getString("code"));
			rmSysUser.setDescript(crs.getString("descript"));
			rmSysUser.setCreatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("createtime"))); 
			rmSysUser.setUpdatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("updatetime")));

			rmSysUser.setEmail(crs.getString("email"));
			rmSysUser.setBirthday(new SimpleDateFormat("yyyy-MM-dd").format(crs.getDate("birthday")));
			rmSysUser.setIdentificationId(crs.getString("identificationId"));
			rmSysUser.setCreateuserObjid(crs.getString("createuserObjid"));
			
			list.add(rmSysUser);
		}
		
		return list;
	}
	
	public RmSysUser getRmSysUsersById(String id) throws SQLException{
		String execSql = "select * from rm_sys_user where objid = ?";
		CachedRowSet crs = DaoUtil.SimpleQuery(execSql,id); 
		RmSysUser rmSysUser  = new RmSysUser();
		if(crs.next()){
			rmSysUser.setObjid(crs.getString("objid"));
			rmSysUser.setDisindex(crs.getString("disindex"));
			rmSysUser.setName(crs.getString("name"));
			rmSysUser.setCode(crs.getString("code"));
			rmSysUser.setDescript(crs.getString("descript"));
			rmSysUser.setCreatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("createtime"))); 
			rmSysUser.setUpdatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("updatetime")));

			rmSysUser.setEmail(crs.getString("email"));
			rmSysUser.setBirthday(new SimpleDateFormat("yyyy-MM-dd").format(crs.getDate("birthday")));
			rmSysUser.setIdentificationId(crs.getString("identificationId"));
			rmSysUser.setCreateuserObjid(crs.getString("createuserObjid"));
		}
		return rmSysUser;
	}
	
	public RmSysUser addRmSysUser(RmSysUser rmSysUser) throws SQLException {
		String execSql = "insert into rm_sys_user(objid,disindex,name,code,descript,createtime,updatetime,email,birthday,identificationId," +
				"createuserObjid) values(?,?,?,?,?,?,?,?,?,?,?)";
		String objid = UUID.randomUUID().toString();
		Object[] params = new Object[]{
				objid,
				new Date().getTime(),
				rmSysUser.getName(),
				rmSysUser.getCode(),
				rmSysUser.getDescript(),
				new Date(),
				new Date(),
				rmSysUser.getEmail(),
				rmSysUser.getBirthday(),
				rmSysUser.getIdentificationId(),
				rmSysUser.getCreateuserObjid()
		};
		DaoUtil.executeUpdate(execSql, params); 
		rmSysUser.setObjid(objid);
		return rmSysUser;
	}
	
	public RmSysUser editRmSysUser(RmSysUser rmSysUser)throws SQLException{
		String execSql = "update rm_sys_user set " +
				"disindex = ? ," +
				"name = ?," +
				"code = ?," +
				"descript = ?," +
				"createtime = ?," +
				"updatetime = ?," +
				"email = ?," +
				"birthday = ?," +
				"identificationId = ?," +
				"createuserObjid = ? " +
				" where objid = ? ";
		Object[] params = new Object[]{
				new Date().getTime(),
				rmSysUser.getName(),
				rmSysUser.getCode(),
				rmSysUser.getDescript(),
				new Date(),
				new Date(),
				rmSysUser.getEmail(),
				rmSysUser.getBirthday(),
				rmSysUser.getIdentificationId(),
				rmSysUser.getCreateuserObjid(),
				rmSysUser.getObjid()
		};
		DaoUtil.executeUpdate(execSql, params); 
		return rmSysUser;
		
	}
	
	public RmSysUser deleteRmSysUser(RmSysUser rmSysUser)throws SQLException{
		String execSql = "delete from rm_sys_user where objid = ? ";
		Object[] params = new Object[]{
				rmSysUser.getObjid()
		};
		DaoUtil.executeUpdate(execSql, params); 
		return rmSysUser;
	}
	
	public static void main(String[] args) throws SQLException {
		//System.out.println();new RmSysUserImpl().getRmSysUsers().size();
		RmSysUser rmSysUser = new RmSysUser ();
		rmSysUser.setObjid("3");
		rmSysUser.setName("xxxxxxxxx");
		rmSysUser.setCode("xxxxxxxxx");
		rmSysUser.setEmail("xxxxxxxxxx");
		rmSysUser.setBirthday("2012-10-09");
		rmSysUser.setCreateuserObjid("1");
		rmSysUser.setIdentificationId("4107031981039283948");
		
		//new RmSysUserImpl().addRmSysUser(rmSysUser);
		
		//new RmSysUserImpl().editRmSysUser(rmSysUser);
		
		new RmSysUserImpl().deleteRmSysUser(rmSysUser);
		
		
	}
	
}
